<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
			<el-row>
				<el-col :span="12">
					<el-form-item label="模块" prop="modulecode" :rules="rules.validateRequire" >
            <tlRefList v-model="model.modulecode" :initDisplayData.sync="model.modulename" dataType="code" :disabled="!isAdd" :singleSelect="true"  queryDataUrl='/appv2/appModuleIcon/queryNoIconModule'></tlRefList>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="图标" prop="moduleicon">
            <input type="file" @change="upimg" accept="image/*" />
					</el-form-item>
				</el-col>
      </el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="" prop="moduleicon" :rules="rules.validateRequire">
            <img :src="model.moduleicon" />
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { dataURLtoBlob, scaleimg } from '@/utils/handlerimg'
import axios from "axios"
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { },
  data() {
    return {
      moduleTitle: '模块图标',
      urlPrefix: '/appv2/appModuleIcon/',
      url: {
        // 额外的url
      }
    }
  },
  methods: {
    upimg(e) {
      let name = e.target.name
      let file = e.target.files[0]
      let filesize = file.size / 1024
      let data = new FileReader()
      let img = new Image()
      let formatdata = ""
      data.onload = e1 => {
        img.onload = e => {
          // 压缩前大小
          if (/^image\/([a-zA-Z]|\*)+$/.test(file.type)) {
            formatdata = scaleimg(e)
            let config = {
              headers: { "Content-Type": "multipart/form-data" }
            }
            let param = new FormData()
            this.$set(this.model, 'moduleicon', formatdata)
            param.append("file", dataURLtoBlob(formatdata))
            axios.post("/uploadimage", param, config).then(res => {
              // 关闭加载动画
              this.$store.commit('showload', false)
              document.getElementById('img').src = res.data
            }).catch(res => { this.$store.commit('SET_showload', false) })
          } else {
            this.$tap.alert('请上传图片格式!')
          }
        }
        img.src = e1.target.result
      }
      this.$store.commit('showload', true)
      data.readAsDataURL(file)
    },
    loaded() {
      // 页面加载完毕执行
    }
  }
}
</script>

